package com.ccf.business.etl.xxljob.config;

import cn.hutool.core.text.StrBuilder;
import com.common.utils.constant.Constants;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.Console;
import java.util.ArrayList;
import java.util.List;

/**
 * xxl-job config
 * @author xuxueli 2017-04-28
 */
@Getter
@Configuration
public class XxlJobConfig {

    @Value("${xxl.job.admin.baseUrl}")
    private String baseUrl;

    @Value("${xxl.job.admin.accessToken}")
    private String accessToken;

    @Value("${xxl.job.admin.loginIdentity}")
    private String loginIdentity;

    @Value("${xxl.job.admin.addJobUrl}")
    private String addJobUrl;

    @Value("${xxl.job.admin.updateJobUrl}")
    private String updateJobUrl;

    @Value("${xxl.job.admin.removeJobUrl}")
    private String removeJobUrl;

    @Value("${xxl.job.admin.stopJobUrl}")
    private String stopJobUrl;

    @Value("${xxl.job.admin.startJobUrl}")
    private String startJobUrl;

    @Value("${xxl.job.admin.triggerJobUrl}")
    private String triggerJobUrl;



    @Value("${xxl.job.executor.appname}")
    private String appName;

    @Value("${xxl.job.executor.address}")
    private String address;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.id}")
    private int id;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;


    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(baseUrl);
        xxlJobSpringExecutor.setAppname(appName);
        xxlJobSpringExecutor.setAddress(address);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }


    /**
     * 获取请求admin需要的cookie
     * @return
     */
    public List<String> getCookieList(){
        List<String> cookies = new ArrayList<>();
        cookies.add(StrBuilder.create().append("XXL_JOB_LOGIN_IDENTITY").append(Constants.EQUALS).append(this.getLoginIdentity()).toString());
        return cookies;
    }
}